//
//  BookmarkViewController.swift
//  HiBook
//
//  Created by Apple on 16/7/27.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import WebKit

class MarkViewController: CommonViewController,WKUIDelegate ,WKNavigationDelegate ,WKScriptMessageHandler {

    var markWebView : WKWebView?
    
    var hidden : String?
    
    var tabFrame : CGRect?
    
    var frameArray = [CGRect]()
    
    let filmVC = UIViewController()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "书评"
        setWebView()
        loadWebView()
        self.view.addSubview(markWebView!)
        
        self.navigationController?.navigationBarHidden = true
        
        self.tabFrame = self.tabBarController?.tabBar.frame
        
        for view in (self.tabBarController?.view.subviews)!{
            self.frameArray.append(view.frame)
        }
    }
    
    
    func loadWebView(){
        
        let request = NSURLRequest(URL: NSURL(string: "http://\(MAIN_IP):8000/web/markView.html")!)
        
        self.markWebView?.loadRequest(request)
        
    }

    func setWebView(){
        
        let config = WKWebViewConfiguration()
        
        self.markWebView = WKWebView(frame: self.view.frame ,configuration: config)
        
        self.markWebView?.UIDelegate = self
        
        self.markWebView?.navigationDelegate = self
        
        self.markWebView?.configuration.userContentController.addScriptMessageHandler(self, name: "responesToPhone")
        
        self.markWebView?.allowsBackForwardNavigationGestures = true
    }
    
    
    func responesToPhone(hidden : String){
        print("刷新成功")
        print(self.tabBarController?.viewControllers)
        
        switch hidden {
        case "true":
            self.tabBarController?.tabBar.hidden = true
            for view in (self.tabBarController?.view.subviews)!{
                view.frame = CGRectZero
            }
        case "false":
            self.tabBarController?.tabBar.hidden = false
            for view in (self.tabBarController?.view.subviews)!{
                var i = 1
                view.frame = frameArray[i]
                i-=1
            }
            
        default:
            let filmView = UIWebView(frame: self.view.frame)
            let button = UIButton(frame: CGRectMake(20,20,25,25))
            button.setImage(UIImage(named: "close"), forState: UIControlState.Normal)
            button.addTarget(self, action: #selector(closeBtn), forControlEvents: UIControlEvents.TouchUpInside)
            filmView.addSubview(button)
            filmView.loadRequest(NSURLRequest(URL: NSURL(string:hidden)!))
            filmVC.view.addSubview(filmView)
            self.presentViewController(filmVC, animated: true, completion: {
                
            })
        }
        
    }
    func closeBtn(){
        self.filmVC.dismissViewControllerAnimated(true) { 
            
        }
    }
    
    func webViewDidStartLoad(webView: UIWebView) {
        NSLog("启动")
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        NSLog("结束")
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        NSLog("error:\(error)")
    }
    
    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
        
//        NSLog("方法名:\(message.name) 传回:\(message.body)")
        
        self.hidden = message.body as? String
        
        self.responesToPhone(hidden!)
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
